#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <random>
#include <map>
#include <set>
using namespace std;
int dp[2020][2020];
int n, m, a[2020];
void smax(int &x, int y) {
	if (x < y) x = y;
}
void solve() {
	scanf("%d",&n);
	for (int i=1;i<=n;++i) scanf("%d",&a[i]);
	for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) dp[i][j]=0;
	for (int i=1;i<=n;++i) {
		for (int j=0;j<i-1;++j) {
			smax(dp[i][i-1], dp[j][i-1]+(a[i]==a[j])*a[i]); // dp[j][i-1]
			smax(dp[j][i], dp[j][i-1]+(a[i]==a[i-1])*a[i]);
			smax(dp[i][j], dp[i-1][j]+(a[i]==a[i-1])*a[i]); // dp[i-1][j]
			smax(dp[i-1][i], dp[i-1][j]+(a[i]==a[j])*a[i]);
		}
	}
	int ans=0;
	for (int i=0;i<=n;++i) {
		smax(ans,dp[i][n]); smax(ans,dp[n][i]);
	}printf("%d\n",ans);
}
int main() {
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int t; scanf("%d",&t);
	while (t--) solve();
	return 0;
}